<?php
/**
 * Author: xx.com
 */ 
namespace Api\Controller;
use Think\Controller;
use Api\Logic\GoodsLogic;
use Think\Page;
use plugins\payment\alipayMobile\alipayMobile;
class PaymentController extends BaseController {
    public $payment; //  具体的支付类
    public $alipay_config = array();// 支付宝支付配置参数
   

    // 使用支付宝支付
    public function dopay(){
        $order_sn = I('order_sn',0);
        if(!$order_sn){
            $res = array('msg'=>'该订单不存在','status'=>-1);
            $this->ajaxReturn($res);
        }
        $order = M('order')->where(array('order_sn'=>$order_sn))->find();
        if(!$order){
            $res = array('msg'=>'该订单不存在','status'=>-1);
            $this->ajaxReturn($res);
        }
        if($order['pay_status'] == 1){
            $this->error('此订单，已完成支付!');
        }
        
        $Alipay = new alipayMobile();
        $str = $Alipay->getMobilePay([
            "order_sn" => $order_sn,
            "subject" => "鲜美特商城订单",
            "body" => "鲜美特商城",
            "total_amount" => $order['order_amount'],
            "show_url" => SITE_URL
        ]);
        $res = array('msg'=>'获取成功','status'=>1,'result'=>$str);
        $this->ajaxReturn($res);
       
    }

    // 使用余额支付
    public function dopayWithMoney(){
        $order_sn = I('order_sn',0);
        if(!$order_sn){
            $res = array('msg'=>'该订单不存在','status'=>-1);
            $this->ajaxReturn($res);
        }
        $order = M('order')->where(array('order_sn'=>$order_sn))->find();
        if(!$order){
            $res = array('msg'=>'该订单不存在','status'=>-1);
            $this->ajaxReturn($res);
        }

        if( $order['user_id'] != $this->user_id )
        {
            $this->ajaxReturn([
                    "msg" => "非法操作",
                    "status" => -1
                ]);
        }

        if($order['pay_status'] == 1){
            $this->ajaxReturn([
                    "msg" => "此订单，已完成支付",
                    "status" => -1
                ]);
        }

        M()->startTrans();
        
        $userInfo = M('users')->where([
                "user_id" => $this->user_id
            ])->field("user_id,user_money")->find();

        if( !$userInfo )
        {
            M()->rollback();
            $this->ajaxReturn([
                    "msg" => "用户信息错误",
                    "status" => -1
                ]);
        }

        if( bccomp( $userInfo['user_money'] ,$order['order_amount'] ) < 0 )
        {
            M()->rollback();
            $this->ajaxReturn([
                    "msg" => "余额不足",
                    "status" => -1
                ]);
        }

        #扣钱&写记录
        $res = accountLog($this->user_id, -1 * $order['order_amount'], 0, '支付订单'.$order['order_sn'], 0, $order['order_id'],$order['order_sn']);

        if(!$res || !$res['is_success'])
        {
            M()->rollback();
            $this->ajaxReturn([
                    "msg" => isset($res['message']) ? $res['message'] : '支付失败-01!',
                    "status" => -1
                ]);
        }

        #完成支付状态
        $order_status = update_pay_status($order_sn,"money","余额",$res['log_id'],$order['order_amount']); // 修改订单支付状态  
    
        if ($order_status===FALSE) {
            M()->rollback(); 
            $this->ajaxReturn([
                    "msg" => "支付失败",
                    "status" => -1
                ]);
        }

        M()->commit();
        $this->ajaxReturn([
                "msg" => "支付成功!",
                "status" => 1,
                "order_sn" => $order_sn
            ]);       
    }

    //充值
    public function doRechangePay($order_id){
        $order = M('recharge')->where(array('order_id'=>$order_id))->find();
        if(!$order){
            $res = array('msg'=>'该充值订单不存在','status'=>-1);
            $this->ajaxReturn($res);
        }
        if($order['pay_status'] == 1){
            $this->error('此充值订单，已完成支付!');
        }
        if($order['pay_status'] == 2){
            $this->error('此充值订单，已关闭!');
        }

        $Alipay = new alipayMobile();

        $str = $Alipay->getMobilePay([
            "order_sn" => $order['order_sn'],
            "subject" => "鲜美特商城充值",
            "body" => "鲜美特商城",
            "total_amount" => $order['account'],
            "show_url" => SITE_URL
        ]);

        $res = array('msg'=>'获取成功','status'=>1,'result'=>$str);
        $this->ajaxReturn($res);
    }

    /**
     * app端发起支付宝,支付宝返回服务器端,  返回到这里
     * http://www.xx.com/index.php/Api/Payment/alipayNotify
     */
    public function alipayNotify()
    {
        $Alipay = new alipayMobile();

        $trade = $Alipay->response();
        
        $order_sn = $trade['order_sn']; //商户订单号
        $trade_no = $trade['trade_no'];//支付宝交易号
        $trade_status = $trade['trade_status'];//交易状态
        $total_fee = $trade['total_fee'];//交易金额

        $order_status = update_pay_status($order_sn,"alipay","支付宝",$trade_no,$total_fee); // 修改订单支付状态  
    
        if ($order_status!==FALSE) {
            echo "success"; //  告诉支付宝支付成功 请不要修改或删除    
        }else{
            echo "fail";
        }
    }
}
